#!/bin/bash -il


# 进入项目所在目录  xxx 为服务器app的代码目录 

# projectPath=$(dirname "$PWD") 
projectPath="/root/jenkins_home/workspace/android/app"
pwd

projectName="rnapp"
work_dir=${projectPath}

cd ${work_dir}

version_dir="${work_dir}/android/app"


#取当前项目下的app版本号文件
cd ${version_dir}
pwd

line=`sed -n '2p' version.properties`

arr=(${line//=/ })  
# arr=(`echo $line | tr ',' ''`) 

versionNumber=${arr[1]}

echo "取到版本号："$versionNumber

cd ${work_dir}/
pwd

rm -rf node_modules/

# 加载初始化react Native所需的modules
npm i  --registry https://registry.npm.taobao.org/

echo $BUILD_ID

Version=`date +%Y%m%d`

#删除组件
cd ${work_dir}/node_modules/
rm -rf react-native-device-info/
rm -rf react-native-rsa-util/
rm -rf react-native-getui/


cd ${work_dir}/
pwd
# 替换第三方依赖组件
cd repair-part
pwd

rm -rf __MACOSX/

unzip react-native-device-info.zip -d ./
unzip react-native-rsa-util.zip -d ./
unzip react-native-getui.zip -d ./

cp -rf ./react-native-device-info/ ../node_modules/react-native-device-info/

cp -rf ./react-native-rsa-util/ ../node_modules/react-native-rsa-util/

cp -rf ./react-native-getui/ ../node_modules/react-native-getui/

rm -rf react-native-device-info/

rm -rf react-native-rsa-util/

rm -rf react-native-getui/

#切回到项目目录
cd ${work_dir}/
pwd

echo "执行Android打包脚本"

cd android

/root/jenkins_home/tools/gradle/gradle-4.4/bin/gradle clean

/root/jenkins_home/tools/gradle/gradle-4.4/bin/gradle aR

rm -rf /usr/share/nginx/html/${projectName}*.apk

cp ${work_dir}/android/app/build/outputs/apk/release/${projectName}-v${versionNumber}-${Version}.apk /usr/share/nginx/html/${projectName}-v${versionNumber}-${Version}.apk

cd /root/jenkins_home/tools/python3/Python-3.6.1

echo "发布二维码"

myqr "http://172.16.23.39:8099/${projectName}-v${versionNumber}-${Version}.apk" -n test_${BUILD_ID}.jpg -d /usr/share/nginx/html/${projectName}-v${versionNumber}-${Version}.apk